<template>
  <div>
    <el-table
      :data="pageData"
      style="width: 100%"
      stripe
      fit
      highlight-current-row
      current-row-key
      :row-style="{ height: '72px' }"
    >
      <el-table-column label="用户 序号" type="index"></el-table-column>
      <el-table-column label="用户名称" prop="name" />
      <el-table-column label="标准分">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            :content="'标准:' + scope.row.standard + '分'"
            placement="right-start"
          >
            <el-tag v-if="90 <= scope.row.standard" class="good">优秀</el-tag>
            <el-tag v-else-if="80 <= scope.row.standard" class="well"
              >良好</el-tag
            >
            <el-tag v-else-if="70 <= scope.row.standard" class="ordinary"
              >一般</el-tag
            >
            <el-tag v-else-if="60 <= scope.row.standard" class="qualified"
              >合格</el-tag
            >
            <el-tag v-else class="fail">不及格</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="诚信分">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            :content="'诚信:' + scope.row.honest + '分'"
            placement="right-start"
          >
            <el-tag v-if="90 <= scope.row.honest" class="good">优秀</el-tag>
            <el-tag v-else-if="80 <= scope.row.honest" class="well"
              >良好</el-tag
            >
            <el-tag v-else-if="70 <= scope.row.honest" class="ordinary"
              >一般</el-tag
            >
            <el-tag v-else-if="60 <= scope.row.honest" class="qualified"
              >合格</el-tag
            >
            <el-tag v-else class="fail">不及格</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="安全分">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            :content="'安全:' + scope.row.safety + '分'"
            placement="right-start"
          >
            <el-tag v-if="90 <= scope.row.safety" class="good">优秀</el-tag>
            <el-tag v-else-if="80 <= scope.row.safety" class="well"
              >良好</el-tag
            >
            <el-tag v-else-if="70 <= scope.row.safety" class="ordinary"
              >一般</el-tag
            >
            <el-tag v-else-if="60 <= scope.row.safety" class="qualified"
              >合格</el-tag
            >
            <el-tag v-else class="fail">不及格</el-tag>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            class="browserButton"
            icon="el-icon-s-opportunity"
            @click="message(scope.row)"
            >扣分详情</el-button
          >
          <el-button
            class="editButton"
            icon="el-icon-edit"
            @click="getDialogVisible(scope.row)"
            >加减分</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <el-dialog :visible.sync="dialogTableVisible" title="扣分详情" size="50%">
      <el-alert
        title="该用户的扣，加分详情"
        :closable="false"
        type="warning"
        center
        show-icon
      />
      <el-table :data="messages">
        <el-table-column type="index" label="消息序号" width="100" />
        <el-table-column property="type" label="扣分类型" />
        <el-table-column property="message" label="扣分信息" />
        <el-table-column property="value" label="扣分数" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button class="deleteButton" @click="deleteMessage(scope.row)"> 删除消息 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <el-dialog :visible.sync="addOrDecrVisible" title="扣分详情" size="50%">
      <el-alert
        title="加、扣分"
        :closable="false"
        type="warning"
        center
        show-icon
      />

      <el-form
        :label-position="labelPosition"
        label-width="80px"
        style="margin-top: 15px"
        :model="addOrDecrData"
      >
        <el-form-item label="扣分消息">
          <el-input v-model="addOrDecrData.message"></el-input>
        </el-form-item>
        <el-form-item label="扣分值">
          <el-input v-model="addOrDecrData.value"></el-input>
        </el-form-item>
        <el-form-item label="扣分类型">
          <el-select v-model="addOrDecrData.type" placeholder="请选择扣分类型">
            <el-option label="标准分" value="standard"></el-option>
            <el-option label="诚信分" value="honest"></el-option>
            <el-option label="安全分" value="safety"></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <el-button class="browserButton" style="margin: 0 auto" @click="addOrDe"
        >确定</el-button
      >
    </el-dialog>
  </div>
</template>

<script>
import {
  pageCredit,
  getCreditMessageById,
  deleteCreditMessage,
  addOrDecr,
} from "@/api/credit";

export default {
  name: "Table",
  data() {
    return {
      total: 0,
      pageData: [],
      loading: true,
      currentPage: 1,
      size: 10,
      messages: [],
      dialogTableVisible: false,
      queryInfo: {
        key: null,
      },
      addOrDecrVisible: false,
      addOrDecrData: {
        id: null,
        message: null,
        value: null,
        type: null,
      },
      labelPosition: "right",
      currentId: null,
    };
  },
  created() {
    this.pageCredit(this.currentPage, this.size);
  },
  methods: {
    pageCredit(current, limit) {
      if (current < 1) {
        return;
      }
      if (this.queryInfo.key != null) {
        // console.log("aaaa");
        let name = this.queryInfo.key;
        pageCredit(current, limit, name).then((res) => {
          this.pageData = res.data.list;
          this.total = res.data.total;
          if (res.data.list.length === 0)
            this.pageCredit(--this.currentPage, this.size);
          this.loading = false;
        });
      } else {
        pageCredit(current, limit).then((res) => {
          this.pageData = res.data.list;
          this.total = res.data.total;
          if (res.data.list.length === 0)
            this.pageCredit(--this.currentPage, this.size);
          this.loading = false;
        });
      }
    },

    getDialogVisible(row) {
      this.addOrDecrVisible = true;
      this.addOrDecrData.id = row.id;
    },

    addOrDe() {
      addOrDecr(this.addOrDecrData).then((res) => {
        this.$message.success("添加加、扣分消息成功!");
        this.pageCredit(this.currentPage, this.size);
        this.addOrDecrVisible = false;
      });
    },

    message(row) {
      this.currentId = row.id;
      getCreditMessageById(row.id).then((res) => {
        this.messages = res.data.list;
        this.dialogTableVisible = true;
      });
    },

    handleCurrentChange(newPage) {
      this.currentPage = newPage;
      this.pageCredit(newPage, this.size);
    },

    handleSizeChange(newSize) {
      this.size = newSize;
      this.pageCredit(this.currentPage, newSize);
    },

    deleteMessage(row) {
      console.log(row);
      this.$confirm("此操作将永久删除该消息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteCreditMessage(row.id).then((res) => {
            console.log(res);
          });
          this.pageCredit(this.currentPage, this.size);
          this.dialogTableVisible = false
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style scoped>
.el-pagination {
  text-align: center;
}
.el-table {
  margin-top: 15px;
  margin-bottom: 15px;
}
.detail {
  background-color: lightseagreen;
  color: #fff;
  border-radius: 10px;
  border: none;
}
.detail:hover {
  background-color: rgb(34, 149, 194);
  color: #fff;
  border-radius: 10px;
  border: none;
}

</style>
